const stockService = require('../services/stockService');

const getStock = async (req, res) => {
  try {
    const stock = await stockService.getStocks(req.query);
    res.status(200).json(stock);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const addStock = async (req, res) => {
  try {
    const newStock = await stockService.addStock(req.body);
    res.status(201).json(newStock);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const updateStock = async (req, res) => {
  try {
    const updatedStock = await stockService.updateStock(req.params.id, req.body);
    res.status(200).json(updatedStock);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const deleteStock = async (req, res) => {
  try {
    await stockService.deleteStock(req.params.id);
    res.status(200).json({ message: `Stock deleted with ID: ${req.params.id}` });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const getSuggestions = async (req, res) => {
  try {
    const suggestions = await stockService.getSuggestions(req.query);
    res.status(200).json(suggestions);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

module.exports = {
  getStock,
  addStock,
  updateStock,
  deleteStock,
  getSuggestions,
};
